package com.blast.cn.ui

import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.blast.cn.R
import com.blast.cn.persenter.more.LightSeekPresenter
import com.blast.cn.persenter.more.VoiceSeekPresenter
import com.wxl.common.AppContext
import com.wxl.common.base.AbsActivity
import com.wxl.common.life.ViewModelQuick
import com.wxl.common.util.DPUtil
import com.wxl.common.util.KeyBoardUtil
import com.wxl.common.util.Toll
import com.wxl.common.wiget.TabSelectorLayout
import kotlinx.android.synthetic.main.activity_more_layout.*

class MoreActivity : AbsActivity() {

    override fun getCreateContentViewId(): Int {
        return R.layout.activity_more_layout
    }

    override fun onCreateViewChanged() {
        setTopTitle("更多")
        ViewModelQuick.observer(this, LightSeekPresenter::class.java, moreLightSeek)
        ViewModelQuick.observer(this, VoiceSeekPresenter::class.java, moreVoiceSeek, moreVoiceText)
        //日间/夜间
        moreDayNightSwitch.onSelected(if (AppContext.appContext.isDayModel()) 0 else 1)
        moreDayNightSwitch.setOnTabSelectorItemClickListener(object :
            TabSelectorLayout.Companion.OnTabSelectorItemClickListener {
            override fun onTabSelectorItemClick(tab: TextView, position: Int) {
                val isChecked = position == 1
//                AppContext.appContext.setDayNight(!isChecked)
//                if (isChecked) {
//                    SkinCompatManager.getInstance()
//                        .loadSkin("night", null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN)
//                } else {
//                    SkinCompatManager.getInstance().restoreDefaultTheme()
//                }
            }

        })


        moreMaxNetBtn.setOnClickListener {
            KeyBoardUtil.hideSoftInput(moreMaxNetEt)
            if (TextUtils.isEmpty(moreMaxNetEt.text)) {
                Toll.toll("请填写最大组网数")
                return@setOnClickListener
            }

            val max = moreMaxNetEt.text.toString().toInt()
            if (max < 1) {
                Toll.toll("最大组网数不能小于1")
                return@setOnClickListener
            }

            if (max > AppContext.appContext.getLeiGuanMaxNum()) {
                Toll.toll("最大组网数不能大于${AppContext.appContext.getLeiGuanMaxNum()}")
                return@setOnClickListener
            }

            AppContext.appContext.saveLeiGuanMaxNum(max)
            Toll.toll("设置成功")
        }

        //正常模式/单机模式
        moreCheckModelWitch.onSelected(if (AppContext.appContext.isCheckModel()) 1 else 0)
        moreCheckModelWitch.setOnTabSelectorItemClickListener(object :
            TabSelectorLayout.Companion.OnTabSelectorItemClickListener {
            override fun onTabSelectorItemClick(tab: TextView, position: Int) {
                val isChecked = position == 1
                AppContext.appContext.setCheckModel(isChecked)
            }
        })


        //显示下传
        moreDownSwitch.onSelected(if (AppContext.appContext.isShowSetUnder()) 1 else 0)
        moreDownSwitch.setOnTabSelectorItemClickListener(object :
            TabSelectorLayout.Companion.OnTabSelectorItemClickListener {
            override fun onTabSelectorItemClick(tab: TextView, position: Int) {
                AppContext.appContext.saveShowSetUnder(position == 1)
            }
        }
        )


        //校时,延时写入
        moreCheckTimeSwitch.onSelected(if(AppContext.appContext.getIsOpenInputDelayData()) 1 else 0)
        moreCheckTimeSwitch.setOnTabSelectorItemClickListener(object : TabSelectorLayout.Companion.OnTabSelectorItemClickListener{
            override fun onTabSelectorItemClick(tab: TextView, position: Int) {
                AppContext.appContext.saveIsOpenInputDelayData(position == 1)
            }
        })

        moreTestModelSwitch.onSelected(AppContext.appContext.getTestModel())
        moreTestModelSwitch.setOnTabSelectorItemClickListener(object :TabSelectorLayout.Companion.OnTabSelectorItemClickListener{
            override fun onTabSelectorItemClick(tab: TextView, position: Int) {
                AppContext.appContext.saveTestModel(position)
            }
        })

    }


    private fun onMenuToggle(isShowMenu: Boolean) {
        val param = moreMenuDividerView.layoutParams as LinearLayout.LayoutParams
        param.bottomMargin = DPUtil.dip2px(if (isShowMenu) 0 else 20)
        moreMenuLayout.visibility = if (isShowMenu) View.VISIBLE else View.GONE
    }

    override fun onKeyDownEvent(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish()
        }
        if (keyCode == KeyEvent.KEYCODE_STAR) {
            onMenuToggle(true)
        }
        if (keyCode == KeyEvent.KEYCODE_POUND) {
            onMenuToggle(false)
        }
        return true
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        KeyBoardUtil.hideSoftInput(moreMaxNetEt)
    }
}